package com.xzlf.mail;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

@Component
public class SimpleMailDemo {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    @Value("${spring.mail.username}")
    private String from;

    public void sendEmail(){
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(from);

        msg.setTo("981136635@qq.com");
        msg.setBcc("752175245@qq.com");
        msg.setSubject("java技术投稿");
        msg.setText("技术分享");
        javaMailSender.send(msg);
    }


    public void sendEmail02() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //是否发送的邮件是富文本（附件，图片，html等）
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo("981136635@qq.com");
        helper.setBcc("752175245@qq.com");
        helper.setSubject("附件邮件");
//        helper.setText("技术分享");
        //重点，默认为false，显示原始html代码，无效果
        helper.setText("欢迎进入<a href=\"http://www.baidu.com\">百度首页</a>", true);
        helper.addAttachment("file", new File("d:/img/122.jpg"));
        javaMailSender.send(mimeMessage);
    }

    public void sendEmail03() throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //是否发送的邮件是富文本（附件，图片，html等）
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
        helper.setFrom(from);
        helper.setTo("981136635@qq.com");
        helper.setBcc("752175245@qq.com");
        helper.setSubject("工作分配");
        Map<String,Object> map = new HashMap<>();
        map.put("username", "李四");
        map.put("num", "1102");
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 5);
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(instance.getTime());

        map.put("salary", dateStr);
        Template template = freeMarkerConfig.getConfiguration().getTemplate("jobTask.ftlh");
        String page = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        helper.setText(page, true);
        javaMailSender.send(mimeMessage);
    }

}
